package com.cdxy.dmr.repository;

import com.cdxy.dmr.dataObject.HotSearchDO;
import com.cdxy.dmr.mapper.HotSearchMapper;
import com.cdxy.model.dto.base.mybatis.BaseRepository;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 热门搜索 服务实现类
 * </p>
 *
 * @author autoGenCode
 * @since 2020-03-05
 */
@Repository
@CacheConfig(cacheNames = "hotel_search")
public class HotSearchRepository extends BaseRepository<HotSearchMapper, HotSearchDO> {

    /**
     * 获取热门标签
     *
     * @param size
     * @return
     */
    public List<HotSearchDO> listOrderByCount(Integer size) {
        return this.baseMapper.selectList(this.queryWrapper()
                .orderByDesc(HotSearchDO::getCount)
                .last("LIMIT " + size));
    }

    /**
     * 根据名字查询
     *
     * @param name
     * @return
     */
    @Cacheable(key = "#name", unless = "#result == null ")
    public HotSearchDO getByName(String name) {
        return this.baseMapper.selectOne(this.queryWrapper()
                .eq(HotSearchDO::getName, name)
                .last("LIMIT 1"));
    }

    /**
     * 热门搜索词加以
     *
     * @param name
     * @return
     */
    public boolean updateSearchCount(String name) {
        return this.baseMapper.updateSearchCount(name);
    }

}
